package bb.lanxing.device.sgdevcie.xpair

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import bb.lanxing.R
import bb.lanxing.databinding.ActivityXpairManagerBinding
import bb.lanxing.device.sgdevcie.xpair.uimodel.XPairUIModel
import bb.lanxing.device.sgdevcie.xpair.viewmodel.XPairViewModel
import bb.lanxing.mvvm.base.BaseDBActivity
import bb.lanxing.mvvm.base.viewmodel.ContextAction.ACTION_SHOW_LOADING
import kotlinx.coroutines.launch

class XPairManagerActivity : BaseDBActivity<ActivityXpairManagerBinding>() {
    private val xPairViewModel by viewModels<XPairViewModel>()

    override fun registerMessageEvent(): Boolean {
        return true
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_xpair_manager
    }

    override fun initView(binding: ActivityXpairManagerBinding) {
        xPairViewModel.xPairUIModel = xPairUIModel
        setupActionBar(true)
        lifecycleScope.launch {
            xPairViewModel.tipsMutableLiveData.observe(this@XPairManagerActivity) { tipAction ->
                snackLong(tipAction.msg, tipAction.isSuccess)
            }
            xPairViewModel.actionLiveData.observe(this@XPairManagerActivity) { contextAction ->
                run {
                    when (contextAction.action) {
                        ACTION_SHOW_LOADING -> {

                        }

                        else -> {

                        }
                    }
                }
            }
        }
    }

//    override fun onBackPressed() {
//        finish()
//    }

    override fun onMessage(i: Int, i2: Int, i3: Int, obj: Any?, bundle: Bundle): Boolean {
        if (i == R.id.msg_change_title) {
            try {
                title = obj as String
                setTitle(title)
            } catch (unused: Exception) {
                return false
            }
            return true
        }
        return false
    }

    companion object {
        private lateinit var xPairUIModel: XPairUIModel
        fun launch(
            launcher: ActivityResultLauncher<Intent>, context: Context, xPairUIModel: XPairUIModel
        ) {
            this.xPairUIModel = xPairUIModel
            launcher.launch(Intent(context, XPairManagerActivity::class.java))
        }
    }
}
